package com.example.demo3.web;

import com.example.demo3.config.MyLog;
import com.example.demo3.domain.User;
import com.example.demo3.domain.bo.UserBo;
import com.example.demo3.domain.vo.UserVo;
import com.example.demo3.mapper.UserMapper;
import com.example.demo3.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("user")
public class UserController {


    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    private  final  UserService userService;

    private final UserMapper userMapper;



    @MyLog
    @RequestMapping("test")
    public List<UserVo> test(String name) {
        UserVo user1 = (UserVo) SecurityUtils.getSubject().getPrincipal();
        log.info("当前登录用户：{}",user1.getUserName());
//        return userMapper.selectByName("提莫");
        return userMapper.selectByName(name);
    }


    @RequestMapping()
    public List<User> list() {

        List<User> result = userService.queryForList();
//        User user = result.get(0);
//        redisTemplate.opsForList().leftPush("list",user);
//        List<User> list = redisTemplate.opsForList().range("list", 0, -1);

        return result;
    }


//    @RequiresPermissions("user:add")
    @PostMapping()
    public String add(@RequestBody UserBo userBo) {
        return " user:add success";
    }

    @RequiresPermissions("user:edit")
    @PutMapping()
    public String edit(@RequestBody UserBo userBo) {

        userService.edit(userBo);
        return " user:edit success";
    }

    @RequiresPermissions("user:remove")
    @DeleteMapping()
    public String remove() {
        return "delete success!";
    }


}
